//
//  GoodsViewController.m
//  Lianxi2
//
//  Created by lanou on 16/7/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "GoodsViewController.h"

@interface GoodsViewController ()<UITableViewDataSource,UITableViewDelegate,SecondDelegate>

@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UITableView *tableView;


@end

@implementation GoodsViewController
#pragma mark---------懒加载----------
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}


- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
#pragma mark---------程序生命周期----------
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.tableView];
    [RequestManager fetchDataWithURL:KGoodsUrl parameter:@{@"auth":@"XMdnEiW0m3qxDCMVzGMTikDJxQ8aoNbKF8W1rUDRicWP23tBNQhpd6fw"} metod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            GoodsModel *model = [[GoodsModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
        
       
        
    } updateUI:^{
        
        [self.tableView reloadData];
    }];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"GoodsModel_cell"];
    
}
#pragma mark---------UITableView的代理----------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodsModel *model = self.dataSource[indexPath.row];
    GoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsModel_cell" forIndexPath:indexPath];
    [cell setCellWithModel:model];
    cell.secendDelegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (void)buyGoosScenesWithUrl:(NSString *)url{
    
    BuyGoosViewController *buy = [[BuyGoosViewController alloc]init];
    buy.url = url;
    NSLog(@"%@",url);
    [self.navigationController pushViewController:buy animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
